package com.xiaohei.question.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xiaohei.question.dao.QuestionMapper;
import com.xiaohei.question.pojo.Question;
import com.xiaohei.question.service.QuestionService;
import entity.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: ZhangYongLiang
 * @Date: 2020/11/15 11:04
 **/
@Service
public class QuestionServiceImpl implements QuestionService {

    private static final String ADMIN_NAME = "XiaoHei";

    @Autowired(required = false)
    private QuestionMapper questionMapper;

    @Override
    public Question randomQuestion() {
        Question question = questionMapper.randomQuestion();
        return question;
    }

    @Override
    public PageResult queryTotal(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Question> questions = questionMapper.queryTotal();
        PageInfo<Question> pageInfo = new PageInfo<>(questions);
        return new PageResult(pageInfo.getTotal(), pageNum, pageSize,questions);
    }

    @Override
    public void add(Question question) {
        question.setLastName(ADMIN_NAME);
        questionMapper.add(question);
    }

    @Override
    public void delete(Integer id) {
        questionMapper.dalete(id);
    }
}
